package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.mapper.TReviewMapper;
import com.esyoga.yoyoga.mapper.TStudentMapper;
import com.esyoga.yoyoga.mapper.TTrainerMapper;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.pojo.DTO.ReviewDTO;
import com.esyoga.yoyoga.service.ReviewService;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Data
@Service("reviewService")
public class ReviewServiceImpl implements ReviewService {
    @Resource
    TReviewMapper tReviewMapper;
    @Resource
    TReviewExample tReviewExample;
    @Resource
    TTrainerExample tTrainerExample;
    @Resource
    TTrainerMapper tTrainerMapper;
    @Resource
    TStudentMapper tStudentMapper;
    @Resource
    TStudentExample tStudentExample;

    @Override
    public int addReview(TReview tReview) {
        return tReviewMapper.insertSelective(tReview);
    }

    @Override
    public List<ReviewDTO> findAllReviewByMid(Integer mid) {
        //存放每条评论对应用户的信息
        List<ReviewDTO> reviewDTOS=new ArrayList<ReviewDTO>();
            tReviewExample.clear();
            TReviewExample.Criteria criteria=tReviewExample.createCriteria();
            //通过动态id查询当前动态下所有的评论
            criteria.andRMIdEqualTo(mid);
            //未删的评论
            criteria.andRFlagEqualTo(0);
            List<TReview>tReviews=tReviewMapper.selectByExample(tReviewExample);
        //System.out.println("11111111"+tReviews);
        for (TReview treview:tReviews
             ) {
            //遍历需要的数据放入dto对象
            ReviewDTO tReview=new ReviewDTO();
            tReview.setRId(treview.getrId());
            tReview.setRUserid(treview.getrUserid());
            tReview.setRUsertype(treview.getrUsertype());
            tReview.setRText(treview.getrText());
            //根据用户类型去对应表查询用户信息
            //如果为学员  0
            if (treview.getrUsertype()==0){
               TStudent tStudent= tStudentMapper.selectByPrimaryKey(treview.getrUserid());
               //如果没有此用户就跳出
               if(tStudent==null){
                   continue;
               }else {
                   tReview.setTStudent(tStudent);
               }
               reviewDTOS.add(tReview);
            }else if(treview.getrUsertype()==1){
                //为私教
               TTrainer tTrainer= tTrainerMapper.selectByPrimaryKey(treview.getrUserid());
               if(tTrainer==null){
                   continue;
               }else {
                   tReview.setTTrainer(tTrainer);
               }
                reviewDTOS.add(tReview);
            }
        }
        return reviewDTOS;
    }
//根据评论id删除当条评论（软删除）
    @Override
    public String delOneReview(Integer rid) {
        int row=0;
        String result="删除失败!";
        tReviewExample.clear();
        TReviewExample .Criteria criteria= tReviewExample.createCriteria();
        criteria.andRIdEqualTo(rid);
        TReview tReview=new TReview();
       tReview.setrFlag(1);
        row= tReviewMapper.updateByExampleSelective(tReview,tReviewExample);
        if(row!=0){
            result="删除成功!";
        }
        return result;

    }
}
